<?php

$id = empty($this->Param['id']) ? 0 : $this->Param['id'];
$this->Assign('id', $id);

$this->LoadHelper('OrderHelper');
$OrderHelper = new OrderHelper();

$this->LoadHelper('EstimateHelper');
$EstimateHelper = new EstimateHelper();

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$this->LoadHelper('BankHelper');
$BankHelper = new BankHelper();
$base_url = \yii2api\Config::$base_url;
$uri = 'chedai/notify/send-estimate-notify';
if (empty($this->Param['option'])) {
    $member_info = $MemberHelper->getMember($this->UserConfig['Id']);
    $ajax = $this->Param['ajax'];
    $page = isset($this->Param['page']) ? $this->Param['page'] : 1;
    $rp = isset($this->Param['rp']) ? $this->Param['rp'] : PAGE_SIZE;
    if($ajax){
        $where = array(
            '`et_status` NOT IN (?)'=> array('作废','退回','已使用','已评估')
        );
        $data = $EstimateHelper->estimateList($where, $page, $rp, $this->Param,$member_info);
        $json_data = array('total' => $data['count'], 'rows' => array());
        if (!empty($data['all']))
        foreach ($data['all'] AS $rowNum => $row) {
            $row['et_stime'] = empty($row['et_stime']) ? '' : date('Y-m-d H:i:s', $row['et_stime']);
            $row['et_ftime'] = empty($row['et_ftime']) ? '' : date('Y-m-d H:i:s', $row['et_ftime']);
            $bank_name = $BankHelper->bankRow($row['et_bank']);
            $row['et_bank_name'] = $bank_name['bk_name'];
            //$row['info_url'] = '/estimate/edit/?id='.$row['et_id'];
            $json_data['rows'][] = $row;
        }
        echo json_encode($json_data);
    }
    else {
        $this->Assign('member_info', $member_info);
        echo $this->GetView('estimate_index.php');
    }
} else {
    switch ($this->Param['option']) {
        case 'eslist':
            $member_info = $MemberHelper->getMember($this->UserConfig['Id']);
            $ajax = $this->Param['ajax'];
            $page = isset($this->Param['page']) ? $this->Param['page'] : 1;
            $rp = isset($this->Param['rp']) ? $this->Param['rp'] : PAGE_SIZE;
            $date=strtotime(date('Ymd'));
            $where = array(
                '`et_status` IN (?)' => array('已评估', '已使用'),
                '`et_ftime`> ?' => $date
            );
            $data = $EstimateHelper->estimateList($where, $page, $rp, $this->Param, $member_info);
            $json_data = array('total' => $data['count'], 'rows' => array());
            if (!empty($data['all']))
                foreach ($data['all'] AS $rowNum => $row) {
                    $row['et_stime'] = empty($row['et_stime']) ? '' : date('Y-m-d H:i:s', $row['et_stime']);
                    $row['et_ftime'] = empty($row['et_ftime']) ? '' : date('Y-m-d H:i:s', $row['et_ftime']);
                    $bank_name = $BankHelper->bankRow($row['et_bank']);
                    $row['et_bank_name'] = $bank_name['bk_name'];
                    $json_data['rows'][] = $row;
                }
            echo json_encode($json_data);
            break;
        case 'etinfo':
            $data = $EstimateHelper->estimateRow($id);
            if($data['et_bank']){
                $bank_name=$BankHelper->bankRow($data['et_bank']);
                $data['et_bank']=$bank_name['bk_name'];
            }
            //$data['attachment'] = $EstimateHelper->attachmentOut($data['et_attach']);
            $data['attachment'] = $OrderHelper->etattachmentOut($data['et_attach']);
            $this->Assign('data', $data);
            echo $this->GetView('estimate_edit.php');
            break;
        case 'remove':
            $id = empty($this->Param['id']) ? ErrorMsg::Debug('参数错误') : $this->Param['id'];
            $result = $EstimateHelper->estimateRemove($id);
            if($result){
                $MemberHelper->saveLog($this->UserConfig, '删除评估', json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('删除成功');
            }
            ErrorMsg::Debug('删除失败');
            break;
        case 'updata':
            $params = $this->Param;
            $edata = $EstimateHelper->estimateRow($id);
            $member_info = $MemberHelper->getMember($this->UserConfig['Id']);
            $this->Assign('member_info', $member_info);
            $arr=array('已使用','已评估');
            if(in_array($edata['et_status'],$arr)){
                ErrorMsg::Debug('当前状态不能修改');
                break;
            }
            $data['et_fname'] = $member_info['u_realname'];
            $data['et_fid'] = $member_info['u_id'];
            $data['et_status']='已评估';
            if($edata['et_price_one']>0){
                $data['et_price_two']=$params['et_price'];
            }else{
                $data['et_price_one']=$params['et_price'];
            }
            $data['et_price']=$params['et_price'];
            $data['et_ftime']=time();
            if($id > 0){
                $result = $EstimateHelper->estimateSave($data, $id);
            }else{
                ErrorMsg::Debug('参数错误');
            }
            if($result['success']){
                $desc = '评估信息';
                $body = array(
                    "m_uid" => $edata['et_sid'],
                    "text" => $edata['et_code'].'-'.$edata['et_name'].'-'.$edata['et_car_model'].'-'.$edata['et_car_date'],
                    "bool" => 1,
                    "remark" => '',
                    "serv_pw" => 'estimate_asdgasdfasd554'
                );
                $reponse = \Httpful\Request::post($base_url.$uri)->body($body)->sendsType(\Httpful\Mime::FORM)->send();
                $MemberHelper->saveLog($this->UserConfig, $desc, json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('保存成功', TRUE);
            }
            ErrorMsg::Debug($result['msg']);
            break;
        case 'return':
            $id = empty($this->Param['id']) ? ErrorMsg::Debug('参数错误') : $this->Param['id'];
            $edata = $EstimateHelper->estimateRow($id);
            $params = $this->Param;
            $member_info = $MemberHelper->getMember($this->UserConfig['Id']);
            $this->Assign('member_info', $member_info);
            $params['et_fname'] = $member_info['u_realname'];
            $params['et_fid'] = $member_info['u_id'];
            $result = $EstimateHelper->estimateRet($params, $id);

            if($result['success']){
                $desc = '评估退回';

                $body = array(
                    "m_uid" => $edata['et_sid'],
                    "text" => $edata['et_code'].'-'.$edata['et_name'].'-'.$edata['et_car_model'].'-'.$edata['et_car_date'],
                    "bool" => 0,
                    "remark" => $params['et_fremarks'],
                    "serv_pw" => 'estimate_asdgasdfasd554'
                );
                $reponse = \Httpful\Request::post($base_url.$uri)->body($body)->sendsType(\Httpful\Mime::FORM)->send();

                $MemberHelper->saveLog($this->UserConfig, $desc, json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('保存成功', TRUE);
            }
            ErrorMsg::Debug($result['msg']);
            break;
        case 'back': //订单退回
            $id = empty($this->Param['id']) ? ErrorMsg::Debug('参数错误') : $this->Param['id'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($id, $this->UserConfig['Id']);
            if (!$order_check['success']) {
                ErrorMsg::Debug('无权操作');
            }

            $this->Assign('data',$this->Param);
            echo $this->GetView('estimate_reback.php');
            break;
        case 'downzip':
            if($this->Param['id']){
                $id =$this->Param['id'];
                $edata = $EstimateHelper->downRow(array('`et_o_number` = ?' => $id));
                $row = $OrderHelper->orderRow($id);
                $rows = $OrderHelper->orderCar($id);

                $car_msg = $this->LoadModel('Public')->getOneRow('order_car','*', array('`ot_oid` = ?' => $id));
                if($car_msg && !empty($car_msg['ot_carcard_img'])){
                    $carcard_img = unserialize($car_msg['ot_carcard_img']);
                    if($carcard_img && $carcard_img['car_image'] && count($carcard_img['car_image'])>0){
                        $edata['et_attach'] = unserialize($edata['et_attach']);
                        $edata['et_attach']['carcard'] = $carcard_img['car_image'];
                        $edata['et_attach'] = serialize($edata['et_attach']);
                    }
                }

                $imgdata = $EstimateHelper->attachUrl($edata['et_attach']);
                $imgdata= $OrderHelper->image_jpg_zip($imgdata,'/temp/estimate_jpg/', 1080);
                $ffname = iconv("utf-8", "gb2312",$row['o_vip_name']);
                $ffname1 = iconv("utf-8", "gb2312",$rows['ot_car_model']);
                $ffname3 = iconv("utf-8", "gb2312",$row['o_branch']);

                $filename='temp/estimate_zip/ysjr'.$ffname.'+'.$ffname1.'+'.substr($rows['ot_initialrg_time'],0,4).'+'.$ffname3.'+'.$rows['ot_assessment'].'.zip';
            }else if($this->Param['etid']){
                $id =$this->Param['etid'];
                $edata = $EstimateHelper->downRow(array('`et_id` = ?' => $id));
                $imgdata = $EstimateHelper->attachUrl($edata['et_attach']);
                $imgdata= $OrderHelper->image_jpg_zip($imgdata,'/temp/estimate_jpg/', 1080);
                $ffname = iconv("utf-8", "gb2312",$edata['et_name']);
                $ffname1 = iconv("utf-8", "gb2312",$edata['et_car_model']);
                $ffname3 = iconv("utf-8", "gb2312",$edata['et_br_name']);
                $filename='temp/estimate_zip/ysjr'.$ffname.'+'.$ffname1.'+'.$ffname3.'.zip';
            }
            \wodrow\tools\Zip::makeDirToZip('temp/estimate_jpg',$filename,$imgdata);
            \wodrow\tools\Zip::download($filename);

            break;
        case'downexcel':
            $id = empty($this->Param['id']) ? ErrorMsg::Debug('参数错误') : $this->Param['id'];
            $row = $OrderHelper->orderRow($id);
            $cert_msg = $this->LoadModel('Public')->getOneRow('order_owner','*',array('`ot_oid` =?' => $id));
            $rows = $OrderHelper->orderCar($id);
            $row=array_merge($row,$rows);
            $row=array_merge($row,$cert_msg);
            $data=array();
            foreach($row as $k=>$v){
                if(in_array($k,array('o_vip_name', 'o_bank_id','ot_home_add','ot_mobile', 'ot_initialrg_time', 'o_vip_card','ot_car_model','ot_calculated','ot_car_price','ot_carno', 'ot_carcolor', 'ot_engineno', 'ot_assessment','ot_car_platenum','ot_loan_limit','ot_loan_year','ot_calculated','ot_cartp','ot_car_register','ot_car_pose'))){
                    $data[$k]=$v;
                }
            }
            $data['ot_calculated'] = unserialize($data['ot_calculated']);
            if($data['o_bank_id']=='15' && $data['ot_car_pose'] == '二手车' ){
              $data['ot_assessment'] = $data['ot_calculated']['d_transaction_amount'];
            }
            $result = $OrderHelper->downCarword($data);
            if($result){
                $OrderHelper->exportReport($id, $this->UserConfig['Id']);
            }
            break;
        default:
    }

}